<?php

use Fisharebest\Webtrees\Http\RequestHandlers\AddChildToIndividualPage;
use Fisharebest\Webtrees\Http\RequestHandlers\AddParentToIndividualPage;
use Fisharebest\Webtrees\Http\RequestHandlers\AddSpouseToIndividualPage;
use Fisharebest\Webtrees\Http\RequestHandlers\LinkChildToFamilyPage;
use Fisharebest\Webtrees\Http\RequestHandlers\LinkSpouseToIndividualPage;
use Fisharebest\Webtrees\Http\RequestHandlers\ReorderFamiliesPage;
use Fisharebest\Webtrees\I18N;

?>
<?php use Fisharebest\Webtrees\View; ?>

<div class="wt-tab-relatives py-4">
    <table class="table table-sm wt-facts-table" role="presentation">
    <tbody>
        <tr>
            <td>
                <label>
                    <input id="show-date-differences" type="checkbox" data-toggle="collapse" data-target=".wt-date-difference">
                    <?= I18N::translate('Date differences') ?>
                </label>
            </td>
        </tr>
    </tbody>
</table>

<?php if ($parent_families->isEmpty() && $can_edit) : ?>
    <table class="table table-sm wt-facts-table" role="presentation">
        <tbody>
            <tr>
                <td>
                    <a href="<?= e(route(AddParentToIndividualPage::class, ['tree' => $individual->tree()->name(), 'xref' => $individual->xref(), 'gender' => 'M'])) ?>">
                        <?= I18N::translate('Add a father') ?>
                    </a>
                </td>
            </tr>
            <tr>
                <td>
                    <a href="<?= e(route(AddParentToIndividualPage::class, ['tree' => $individual->tree()->name(), 'xref' => $individual->xref(), 'gender' => 'F'])) ?>">
                        <?= I18N::translate('Add a mother') ?>
                    </a>
                </td>
            </tr>
        </tbody>
    </table>
<?php endif ?>

<!-- Parents -->
<?php foreach ($parent_families as $family) : ?>
    <?= view('modules/relatives/family', [
        'individual'       => $individual,
        'family'           => $family,
        'type'             => 'FAMC',
        'label'            => $individual->getChildFamilyLabel($family),
        'fam_access_level' => $fam_access_level,
    ]) ?>
<?php endforeach ?>

<!-- step-parents -->
<?php foreach ($step_parent_families as $family) : ?>
    <?= view('modules/relatives/family', [
        'individual'       => $individual,
        'family'           => $family,
        'type'             => 'FAMC',
        'label'            => $individual->getStepFamilyLabel($family),
        'fam_access_level' => $fam_access_level,
    ]) ?>
<?php endforeach ?>

<!-- spouses -->
<?php foreach ($spouse_families as $family) : ?>
    <?= view('modules/relatives/family', [
        'individual'       => $individual,
        'family'           => $family,
        'type'             => 'FAMS',
        'label'            => $individual->getSpouseFamilyLabel($family),
        'fam_access_level' => $fam_access_level,
    ]) ?>
<?php endforeach ?>

<!-- step-children -->
<?php foreach ($step_child_families as $family) : ?>
    <?= view('modules/relatives/family', [
        'individual'       => $individual,
        'family'           => $family,
        'type'             => 'FAMS',
        'label'            => $family->fullName(),
        'fam_access_level' => $fam_access_level,
    ]) ?>
<?php endforeach ?>

<?php if ($can_edit) : ?>
    <br>
    <table class="table table-sm wt-facts-table" role="presentation">
        <tbody>
            <?php if ($spouse_families->count() > 1) : ?>
                <tr>
                    <td>
                        <a href="<?= e(route(ReorderFamiliesPage::class, ['tree' => $individual->tree()->name(), 'xref' => $individual->xref()])) ?>">
                            <?= view('icons/reorder') ?>
                            <?= I18N::translate('Re-order families') ?>
                        </a>
                    </td>
                </tr>
            <?php endif ?>
            <tr>
                <td>
                    <a href="<?= e(route(LinkChildToFamilyPage::class, ['tree' => $individual->tree()->name(), 'xref' => $individual->xref()])) ?>">
                        <?= I18N::translate('Link this individual to an existing family as a child') ?>
                    </a>
                </td>
            </tr>

            <tr>
                <td>
                    <a href="<?= e(route(AddSpouseToIndividualPage::class, ['tree' => $individual->tree()->name(), 'xref' => $individual->xref()])) ?>">
                        <?php if ($individual->sex() !== 'F') : ?>
                            <?= I18N::translate('Add a wife') ?>
                        <?php else : ?>
                            <?= I18N::translate('Add a husband') ?>
                        <?php endif ?>
                    </a>
                </td>
            </tr>

            <tr>
                <td>
                    <a href="<?= e(route(LinkSpouseToIndividualPage::class, ['tree' => $individual->tree()->name(), 'xref' => $individual->xref()])) ?>">
                        <?php if ($individual->sex() !== 'F') : ?>
                            <?= I18N::translate('Add a wife using an existing individual') ?>
                        <?php else : ?>
                            <?= I18N::translate('Add a husband using an existing individual') ?>
                        <?php endif ?>
                    </a>
                </td>
            </tr>

            <tr>
                <td>
                    <a href="<?= e(route(AddChildToIndividualPage::class, ['tree' => $individual->tree()->name(), 'xref' => $individual->xref(), 'gender' => 'U'])) ?>">
                        <?= I18N::translate('Add a child to create a one-parent family') ?>
                    </a>
                </td>
            </tr>
        </tbody>
    </table>
<?php endif ?>
</div>

<?php View::push('javascript') ?>
<script>
    'use strict';

    webtrees.persistentToggle("show-date-differences");
</script>
<?php View::endpush() ?>
